package mytool;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;
import java.util.Scanner;

import jdbc.DBUtil;

public class Pinyin {
	public static void main(String[] args) throws Exception {
		InputStream in = Pinyin.class
				.getResourceAsStream("/mytool/duizhaobiao.properties");
		Properties props = new Properties();
		props.load(in);
		Scanner sc = new Scanner(System.in);
		String input = sc.nextLine();
		String[] pinyings=input.split(" ");//一个或多个空格
		String[] hanzis=new String[pinyings.length];
		for (int i = 0; i < hanzis.length; i++) {
			hanzis[i]=props.getProperty(pinyings[i]);//如果输入 he w,第二个元素为空，后面会出现空指针异常
		}
		String sql = "select string from pinyinghanzi ";
		Connection con = DBUtil.openInThread();
		PreparedStatement pst = con.prepareStatement(sql);
		ResultSet rs = pst.executeQuery();
		while(rs.next()){
			String name=rs.getString("string");
			search(name,hanzis);
		}
	}
	
	/**
	 * 输入的拼音所代表的字应该是相连的
	 */
	public static void search(String name,String[] hanzis){
		//布尔变量，控制是否输出名字
		boolean print=false;
		//布尔变量，控制是否继续查找
		boolean continueSearch=true;
		
		int startPoint=0;
			for (int j = 0; j <= name.length()-hanzis.length; j++) {
				String hanzi=String.valueOf(name.charAt(j));
				if(hanzis[0].contains(hanzi)){
					startPoint=j;
					continueSearch=true;
					break;
				}
				//循环执行的最后一次，没找到就不继续找了
				if(j==name.length()-hanzis.length){
					continueSearch=false;
				}
			}
			
		//找到输入的第一个拼音所代表的字
		if(continueSearch){
			print=searchOther(name, hanzis, startPoint);
		}
		if(print){
			System.out.println(name);
		}
	}
	public static boolean searchOther(String name,String[] hanzis,int startPoint){
		boolean print=true;
		for (int i = 1; i < hanzis.length; i++) {
			int idx=++startPoint;
			if(idx>name.length()-1){
				print=false;
				break;
			}
			if(!hanzis[i].contains(String.valueOf(name.charAt(idx)))){
				print=false;
				break;
			}
		}
		return print;
	}
	
	
	/**
	 * 用输入的拼音得到的汉字串数组去与数据库中存在的名字匹配
	 * 输入he tai也能找到贺安泰
	 * 此方法用来找出一个名字中是否包括输入的拼音所对应的字
	 */
	public static void search(String[] hanzis,String name){
		//布尔变量，控制是否输出名字
		boolean print=false;
		//布尔变量，控制是否继续查找
		boolean continueSearch=true;
		
		int startPoint=0;
		for (int i = 0; i < hanzis.length; i++) {
			//j的开始与结束都是变量
			for (int j = startPoint; j <= name.length()-hanzis.length+i; j++) {
				String hanzi=String.valueOf(name.charAt(j));
				if(hanzis[i].contains(hanzi)){
					startPoint=j+1;
					break;
				}
				//循环执行的最后一次，没找到就不继续找了
				if(j==name.length()-hanzis.length+i){
					continueSearch=false;
				}
			}
			if(!continueSearch){
				break;
			}
			//循环执行的最后一次，说明可以输出名字
			if(i==hanzis.length-1){
				print=true;
			}
		}
		if(print){
			System.out.println(name);
		}
	}
}
